package com.njbdqn.updown.ctrl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njbdqn.updown.service.StuService;
import com.njbdqn.updown.vo.Student;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class StuCtrl {
    @Autowired
    private StuService stuService;
    @InitBinder
    public void dataBind(WebDataBinder wdb){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(true);
        wdb.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
    }
    @RequestMapping("/upload.do")
    public String up(@RequestParam("file")MultipartFile file,
                     Student stu, HttpSession session)throws Exception{
        String path=session.getServletContext().getRealPath("/upload");
        File fp=new File(path);
        System.out.print(path);
        if(!fp.exists()){
            fp.mkdirs();
        }
        String newName=stuService.getNewName(file.getOriginalFilename());
        File saveFile=new File(path,newName);
        file.transferTo(saveFile);
        stu.setPhoto(newName);
        stuService.save(stu);
        return "redirect:/list.do";
    }
    //遍历文件夹目录并显示在页面上
    @RequestMapping("/list.do")
    public String foreachDirectory(HttpServletRequest request){
        String path=request.getSession().getServletContext().getRealPath("/upload");
        File file=new File(path);
        request.setAttribute("fs",file.list());
        return "list.jsp";
    }
    //down
    @RequestMapping("/download.do")
    public String down(String filename, HttpSession session, HttpServletResponse response)
    throws Exception{
        //获取文件路径
        String path=session.getServletContext().getRealPath("/upload");
        //构建文件
        File downFile=new File(path,filename);

        //设置页面不进行跳转而去下载的响应头
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition","attachment;fileName="+filename);
        //通过io流回写数据给客户端
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(downFile));
        byte[] bs=new byte[bis.available()];
        bis.read(bs);
        bis.close();
        OutputStream out=response.getOutputStream();
        out.write(bs);
        out.flush();
        out.close();
        return  null;

    }
    @RequestMapping("/page.do")
    public String page(@RequestParam(required = false,defaultValue = "1") int cp,HttpServletRequest request){
        PageHelper .startPage(cp,3);
        List<Student> stus=stuService.findAll();
        request.setAttribute("pb",new PageInfo<Student>(stus));
        return "page.jsp";
    }
}
